﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Shared.Service.DataPermissions
{
    /// <summary>
    /// 门店数据权限范围
    /// </summary>
    public class SchoolDataPermission: DataPermission<string, SchoolDataPermission>
    {
        /// <summary>
        /// 没启用的
        /// </summary>
        public static readonly SchoolDataPermission Disable = new SchoolDataPermission(DataPermissionState.Disable);

        /// <summary>
        /// Null 的门店数据范围内为
        /// </summary>
        public static readonly SchoolDataPermission Null = new SchoolDataPermission(DataPermissionState.Null);

        /// <summary>
        /// 所有的门店
        /// </summary>
        public static readonly SchoolDataPermission All = new SchoolDataPermission(DataPermissionState.All);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="state"></param>
        protected SchoolDataPermission(DataPermissionState state) : base(state)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schoolIds"></param>
        public SchoolDataPermission(string[] schoolIds)
            : base(schoolIds)
        {

        }

        /// <summary>
        /// 以当前数据权限为基础，产生一个新的数据权限，
        /// 考虑用户输入的权限 默认交集计算
        /// </summary>
        /// <param name="inputIds"></param>
        /// <param name="disableAs">DisableAs 处理方式</param>
        /// <returns></returns>
        public override SchoolDataPermission GetInputDataPermission(string[] inputIds,
            DisableAs disableAs = DisableAs.All)
        {
            if (inputIds.IsNullOrEmpty())
            {
                return this;
            }

            switch (State)
            {
                case DataPermissionState.Disable:
                    return disableAs switch
                    {
                        DisableAs.All => new SchoolDataPermission(inputIds),
                        _ => Null
                    };
                case DataPermissionState.Null:
                    return Null;
                case DataPermissionState.Normal:
                    var intersectIds = Ids.Intersect(inputIds).ToArray(); // 取交集
                    return intersectIds.Any() ? new SchoolDataPermission(intersectIds) : Null;
                case DataPermissionState.All:
                    return new SchoolDataPermission(inputIds);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        /// <summary>
        /// 是否拥有门店数据权限
        /// </summary>
        /// <param name="schoolId"></param>
        /// <returns></returns>
        public bool HasDataPermission(string schoolId)
        {
            if (schoolId.IsNullOrWhiteSpace())
            {
                return false;
            }

            return State switch
            {
                DataPermissionState.All => true,
                DataPermissionState.Normal => Ids.Contains(schoolId),
                _ => false,
            };
        }
    }
}
